package com.group12.liuxinpeng.role.controller;

import com.group12.liuxinpeng.common.control.BaseController;
import com.group12.liuxinpeng.mybatis.entity.PtRoleEntity;
import com.group12.liuxinpeng.mybatis.flexigrid.PadingRstType;
import com.group12.liuxinpeng.mybatis.flexigrid.PagingBean;
import com.group12.liuxinpeng.mybatis.ztree.ZtreeEntity;
import com.group12.liuxinpeng.role.entity.SearchRoleEntity;
import com.group12.liuxinpeng.role.service.RoleService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {
    private Logger logger = LogManager.getLogger(this.getClass());
    @Resource
    private RoleService roleService;

    @RequestMapping("/rolePage")
    public String rolePage(){
        return "role/rolePage";
    }

    @RequestMapping("/queryRoleListPage")
    @ResponseBody
    public PadingRstType<PtRoleEntity> queryRoleListPage(SearchRoleEntity search, PagingBean paging){
        logger.info(search);
        logger.info(paging);
        PadingRstType<PtRoleEntity> ptRoleEntityPadingRstType = roleService.queryRoleListPage(search,paging);
        return ptRoleEntityPadingRstType;
    }


    @RequestMapping("/getMenuZtreeListByRoleId")
    @ResponseBody
    public List<ZtreeEntity> getMenuByRoleId(@RequestParam("roleUuid") String roleUuid){
        logger.info("roleUuid:" + roleUuid);
        List<ZtreeEntity> ztreeStrEntities = roleService.queryMenuByRoleId(roleUuid);

        return ztreeStrEntities;
    }

    @RequestMapping("/saveMenuIdAndRoleId")
    @ResponseBody
    public String saveMenuIdAndRoleId(@RequestParam("roleUuid") String roleUuid,
                                      @RequestParam("menuIds") String menuIds){
        logger.info("roleUuid:" + roleUuid);
        logger.info("menuArray:" + menuIds);
        roleService.saveMenuIdAndRoleId(roleUuid,menuIds);
        return getSuccessMessage("role.menu.allocate.success");
    }

}
